<template>
  <div>
    <el-form label-position="right" v-model="eduTeacher" label-width="15%">
      <el-form-item label="讲师姓名">
        <el-col :span="8">
          <el-input v-model="eduTeacher.name" placeholder="小跳"></el-input>
        </el-col>
      </el-form-item>
      <el-form-item label="讲师排序">
        <el-col :span="8">
          <el-input-number
            v-model="eduTeacher.sort"
            controls-position="right"
            :min="0"
          ></el-input-number>
        </el-col>
      </el-form-item>
      <el-form-item label="讲师头衔">
        <el-col :span="8">
          <el-select placeholder="请选择" v-model="eduTeacher.level">
            <el-option :value="1" label="高级讲师"></el-option>
            <el-option :value="2" label="首席讲师"></el-option>
          </el-select>
        </el-col>
      </el-form-item>
      <el-form-item label="讲师资历">
        <el-col :span="8">
          <el-input
            v-model="eduTeacher.career"
            placeholder="如 :  博士生、研究生 "
          ></el-input>
        </el-col>
      </el-form-item>

      <el-form-item label="讲师简介">
        <el-col :span="8">
          <el-input
            placeholder="如 :  执教于西昌学院"
            :rows="10"
            type="textarea"
            v-model="eduTeacher.intro"
          ></el-input>
        </el-col>
      </el-form-item>

      <!-- TOOD 头像上传 -->
      <el-form-item label="讲师头像">
        <!-- 头衔缩略图 -->
        <pan-thumb :image="eduTeacher.avatar" />
        <!-- 文件上传按钮 -->
        <el-button
          type="primary"
          icon="el-icon-upload"
          @click="imagecropperShow = true"
          >更换头像
        </el-button>
        <!--
          v-show：是否显示上传组件
          :key：类似于id，如果一个页面多个图片上传控件，可以做区分
          :url：后台上传的url地址
          @close：关闭上传组件
          @crop-upload-success：上传成功后的回调 -->
        <image-cropper
          v-show="imagecropperShow"
          :width="300"
          :height="300"
          :key="imagecropperKey"
          :url="BASE_API + '/eduoss/fileoss/upload'"
          field="file"
          @close="close"
          @crop-upload-success="cropSuccess"
        />
      </el-form-item>

      <el-form-item>
        <el-button
          :disabled="saveBtnDisabled"
          type="primary"
          @click="saveAndUpdate"
          >保存</el-button
        >
      </el-form-item>
    </el-form>
  </div>
</template>

<script>
import teacher from "@/api/edu/teacher";
import ImageCropper from "@/components/ImageCropper";
import PanThumb from "@/components/PanThumb";
export default {
  name: "SaveTeacher",
  components: { ImageCropper, PanThumb },
  data() {
    return {
      eduTeacher: {
        name: "",
        sort: 0, //排序
        level: 1, //头衔
        career: "", //资历
        intro: "", //简介
        avatar: "",
      },

      imagecropperKey: 0, //默认key为0
      BASE_API: process.env.BASE_API, //基本API
      imagecropperShow: false, //默认上传显示false
      saveBtnDisabled: false, //保存按钮是否禁用
    };
  },
  created() {
    //判断url中有没有id值 然后立马回显数据进行展示
    this.init();
  },
  watch: {
    //路由变化 方法执行 将eduTeacher清空
    $route(to, from) {
      console.log("init");
      this.init();
    },
  },
  methods: {
    //关闭上传弹框
    close() {
      this.imagecropperShow = false;
      this.imagecropperKey = this.imagecropperKey + 1; //把key 加一
    },
    //上传成功
    cropSuccess(data) {
      console.log(data);
      this.imagecropperShow = false;
      this.eduTeacher.avatar = data.picUrl;
      this.imagecropperKey = this.imagecropperKey + 1;
    },

    //初始化页面 防止页面冲突数据不会改变
    init() {
      if (this.$route.params && this.$route.params.id) {
        const id = this.$route.params.id;
        this.getTeacherInfoById(id);
      } else {
        //如果放在create里面 一个Vue组件只能执行一次 所以不能刷新
        this.eduTeacher = {};
      }
    },

    //调用添加或修改的操作
    saveAndUpdate() {
      //判断是修改还是添加
      if (this.eduTeacher.id) {
        this.updateTeacherById(this.eduTeacher.id);
      } else {
        this.saveTeacher();
      }
    },

    //添加讲师
    saveTeacher() {
      //调用 api
      teacher.saveTeacher(this.eduTeacher).then((response) => {
        this.$message({
          type: "success",
          message: "添加成功!",
        });

        //路由跳转
        this.$router.push({ path: "/teacher/table" });
      });
    },

    //修改讲师
    //1 查询讲师
    getTeacherInfoById(id) {
      teacher.findTeacherById(id).then((response) => {
        this.eduTeacher = response.data;
        console.log(this.eduTeacher);
      });
    },

    //2 修改讲师信息
    updateTeacherById() {
      teacher.updateTeacherById(this.eduTeacher).then((response) => {
        this.$message({
          type: "success",
          message: "修改成功!",
        });
        //跳转到主页
        this.$router.push({ path: "/teacher/table" });
      });
    },
  },
};
</script>

<style>
</style>